<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="Linux 文件的概念，以及对文件I/O操作的接口。">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux文件与I&#x2F;O">
<meta property="og:url" content="http://luqilinok.github.io/2019/07/11/linuxfileio/index.html">
<meta property="og:site_name" content="Lu QiLin&#39;s Blog">
<meta property="og:description" content="Linux 文件的概念，以及对文件I/O操作的接口。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://luqilinok.github.io/2019/07/11/linuxfileio/fileshare_1.png">
<meta property="og:image" content="http://luqilinok.github.io/2019/07/11/linuxfileio/fileshare_2.png">
<meta property="og:image" content="http://luqilinok.github.io/2019/07/11/linuxfileio/fileshare_3.png">
<meta property="og:image" content="http://luqilinok.github.io/2019/07/11/linuxfileio/file_dup.png">
<meta property="og:updated_time" content="2019-07-11T07:34:30.511Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Linux文件与I&#x2F;O">
<meta name="twitter:description" content="Linux 文件的概念，以及对文件I/O操作的接口。">
<meta name="twitter:image" content="http://luqilinok.github.io/2019/07/11/linuxfileio/fileshare_1.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://luqilinok.github.io/2019/07/11/linuxfileio/">





  <title>Linux文件与I/O | Lu QiLin's Blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Lu QiLin's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-technology">
          <a href="/categories/Technology/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Technology
          </a>
        </li>
      
        
        <li class="menu-item menu-item-notes">
          <a href="/categories/Notes/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            Notes
          </a>
        </li>
      
        
        <li class="menu-item menu-item-personal">
          <a href="/categories/Personal/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            Personal
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            About
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://luqilinok.github.io/2019/07/11/linuxfileio/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="LuQiLin">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lu QiLin's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Linux文件与I/O</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-07-11T14:19:33+08:00">
                2019-07-11
              </time>
            

            

            
          </span>
          
  <span class="post-updated">
    &nbsp; | &nbsp; 更新于
    <time itemprop="dateUpdated" datetime="2019-07-11T15:34:30+08:00" content="2019-07-11">
      2019-07-11
    </time>
  </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Notes/" itemprop="url" rel="index">
                    <span itemprop="name">Notes</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>Linux 文件的概念，以及对文件I/O操作的接口。</p>
<a id="more"></a>
<hr>
<h1 id="什么是I-O？"><a href="#什么是I-O？" class="headerlink" title="什么是I/O？"></a>什么是I/O？</h1><ul>
<li>输入/输出是主存和外部设备之间拷贝数据的过程<ul>
<li>设备-&gt;内存 （输入操作）</li>
<li>内存-&gt;设备 （输出操作）</li>
</ul>
</li>
<li>高级I/O<ul>
<li>ANSI C提供的标准I/O库称为高级I/O，通常也称为带缓冲的I/O</li>
<li>低级I/O</li>
</ul>
</li>
<li>通常也称为不带缓冲的I/O</li>
</ul>
<hr>
<h1 id="文件描述符"><a href="#文件描述符" class="headerlink" title="文件描述符"></a>文件描述符</h1><ul>
<li>对于Linux而言，所有对设备或文件的操作都是通过文件描述符进行的。</li>
<li>当打开或者创建一个文件的时候，内核向进程返回一个文件描述符（非负整数）。后续对文件的操作只需通过该文件描述符，内核记录有关这个打开文件的信息。</li>
<li>一个进程启动时，默认打开了3个文件，标准输入、标准输出、标准错误，对应文件描述符是0（STDIN_FILENO）、1（STDOUT_FILENO）、2（STDERR_FILENO）,这些常量定义在unistd.h头文件中。</li>
</ul>
<hr>
<h1 id="文件共享"><a href="#文件共享" class="headerlink" title="文件共享"></a>文件共享</h1><p><img src="/2019/07/11/linuxfileio/fileshare_1.png" alt></p>
<p><img src="/2019/07/11/linuxfileio/fileshare_2.png" alt></p>
<p><img src="/2019/07/11/linuxfileio/fileshare_3.png" alt></p>
<hr>
<h1 id="文件描述符与文件指针转换"><a href="#文件描述符与文件指针转换" class="headerlink" title="文件描述符与文件指针转换"></a>文件描述符与文件指针转换</h1><ul>
<li>fileno：将文件指针转换为文件描述符</li>
<li>fdopen：将文件描述符转换为文件指针</li>
</ul>
<p>实例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"fileno(stdin) = %d\n"</span>, fileno(<span class="built_in">stdin</span>));</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<h1 id="文件调用系统"><a href="#文件调用系统" class="headerlink" title="文件调用系统"></a>文件调用系统</h1><h2 id="open函数"><a href="#open函数" class="headerlink" title="open函数"></a>open函数</h2><p>功能：打开或者打开并创建文件</p>
<p>原型：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">open</span><span class="params">(<span class="keyword">const</span> <span class="keyword">char</span> *path, <span class="keyword">int</span> flags)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">open</span><span class="params">(<span class="keyword">const</span> <span class="keyword">char</span> *path, <span class="keyword">int</span> flags,<span class="keyword">mode_t</span> mode)</span></span>;</span><br></pre></td></tr></table></figure>
<p>参数：</p>
<ul>
<li>path ：文件的名称，可以包含（绝对和相对）路径</li>
<li>flags：文件打开模式</li>
<li>mode:  用来规定对该文件的所有者，文件的用户组及系统中其他用户的访问权限</li>
</ul>
<p>返回值：打开成功，返回文件描述符；打开失败，返回－1</p>
<p>打开文件的方式：</p>
<table>
<thead>
<tr>
<th style="text-align:left">打开方式</th>
<th style="text-align:left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">O_RDONLY</td>
<td style="text-align:left">打开一个供读取的文件</td>
</tr>
<tr>
<td style="text-align:left">O_WRONLY</td>
<td style="text-align:left">打开一个供写入的文件</td>
</tr>
<tr>
<td style="text-align:left">O_RDWR</td>
<td style="text-align:left">打开一个可供读写的文件</td>
</tr>
<tr>
<td style="text-align:left">O_APPEND</td>
<td style="text-align:left">写入的所有数据将被追加到文件的末尾</td>
</tr>
<tr>
<td style="text-align:left">O_CREAT</td>
<td style="text-align:left">打开文件，如果文件不存在则建立文件</td>
</tr>
<tr>
<td style="text-align:left">O_EXCL</td>
<td style="text-align:left">如果已经置O_CREAT且文件存在，则强制open()失败</td>
</tr>
<tr>
<td style="text-align:left">O_TRUNC</td>
<td style="text-align:left">在open（）时，将文件的内容清空</td>
</tr>
</tbody>
</table>
<p>访问权限：</p>
<table>
<thead>
<tr>
<th style="text-align:left">打开方式</th>
<th style="text-align:left">描述</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">S_IRUSR</td>
<td style="text-align:left">文件所有者的读权限位</td>
<td></td>
<td></td>
</tr>
<tr>
<td style="text-align:left">S_IWUSR</td>
<td style="text-align:left">文件所有者的写权限位</td>
<td></td>
<td></td>
</tr>
<tr>
<td style="text-align:left">S_IXUSR</td>
<td style="text-align:left">文件所有者的执行权限位</td>
<td></td>
<td></td>
</tr>
<tr>
<td style="text-align:left">S_IRWXU</td>
<td style="text-align:left">S_IRUSR</td>
<td>S_IWUSR</td>
<td>S_IXUSR</td>
</tr>
<tr>
<td style="text-align:left">S_IRGRP</td>
<td style="text-align:left">文件用户组的读权限位</td>
<td></td>
<td></td>
</tr>
<tr>
<td style="text-align:left">S_IWGRP</td>
<td style="text-align:left">文件用户组的写权限位</td>
<td></td>
<td></td>
</tr>
<tr>
<td style="text-align:left">S_IXGRP</td>
<td style="text-align:left">文件用户组的执行权限位</td>
<td></td>
<td></td>
</tr>
<tr>
<td style="text-align:left">S_IRWXG</td>
<td style="text-align:left">S_IRGRP</td>
<td>S_IWGRP</td>
<td>S_IXGRP</td>
</tr>
<tr>
<td style="text-align:left">S_IROTH</td>
<td style="text-align:left">文件其他用户的读权限位</td>
<td></td>
<td></td>
</tr>
<tr>
<td style="text-align:left">S_IWOTH</td>
<td style="text-align:left">文件其他用户的写权限位</td>
<td></td>
<td></td>
</tr>
<tr>
<td style="text-align:left">S_IXOTH</td>
<td style="text-align:left">文件其他用户的执行权限位</td>
<td></td>
<td></td>
</tr>
<tr>
<td style="text-align:left">S_IRWXO</td>
<td style="text-align:left">S_IROTH</td>
<td>S_IWOTH</td>
<td>S_IXOTH</td>
</tr>
</tbody>
</table>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/types.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/stat.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;fcntl.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ERR_EXIT(m) \</span></span><br><span class="line">        <span class="keyword">do</span> \</span><br><span class="line">        &#123; \</span><br><span class="line">                perror(m); \</span><br><span class="line">                <span class="built_in">exit</span>(EXIT_FAILURE); \</span><br><span class="line">        &#125; <span class="keyword">while</span>(<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="keyword">int</span> fd = open(<span class="string">"test"</span>, O_RDONLY | O_CREAT, <span class="number">0666</span>);</span><br><span class="line">	<span class="keyword">if</span> (fd == <span class="number">-1</span>) &#123;</span><br><span class="line">		ERR_EXIT(<span class="string">"open"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="close函数"><a href="#close函数" class="headerlink" title="close函数"></a>close函数</h2><ul>
<li><p>功能：释放打开的文件描述符 </p>
</li>
<li><p>原型：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int close(int fd);</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数：</p>
<ul>
<li>fd ：要关闭的文件的文件描述符</li>
</ul>
</li>
<li><p>返回值：如果出现错误，返回-1；调用成功返回0</p>
</li>
</ul>
<hr>
<h2 id="create函数"><a href="#create函数" class="headerlink" title="create函数"></a>create函数</h2><ul>
<li><p>功能：为了维持与早期的UNIX系统的向后兼容性，Linux也提供可选的创建文件的系统调用，它称为creat()。</p>
</li>
<li><p>原型：</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int creat(const char *path, mode_t mode)；</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数：</p>
<ul>
<li>path ：文件的名称，可以包含（绝对和相对）路径</li>
<li>mode:  用来规定对该文件的所有者，文件的用户组及系统中其他用户的访问权限</li>
</ul>
</li>
<li><p>返回值：打开成功，返回文件描述符；打开失败，返回－1</p>
</li>
</ul>
<hr>
<h2 id="read函数"><a href="#read函数" class="headerlink" title="read函数"></a>read函数</h2><ul>
<li><p>从文件中读取字节 </p>
</li>
<li><p>原型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ssize_t</span> read(<span class="keyword">int</span> fd, <span class="keyword">void</span> *buf, <span class="keyword">size_t</span> count);</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数:</p>
<ul>
<li>fd ：想要读的文件的文件描述符</li>
<li>buf ： 指向内存块的指针，从文件中读取来的字节放到这个内存块中</li>
<li>count ： 从该文件复制到buf中的字节个数</li>
</ul>
</li>
<li><p>返回值 如果出现错误，返回-1，读文件结束，返回0，否则返回从该文件复制到规定的缓冲区中的字节数</p>
</li>
</ul>
<hr>
<h2 id="write函数"><a href="#write函数" class="headerlink" title="write函数"></a>write函数</h2><ul>
<li><p>将数据写到一个文件中 </p>
</li>
<li><p>原型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ssize_t</span> write(<span class="keyword">int</span> fd, <span class="keyword">const</span> <span class="keyword">void</span> *buf, <span class="keyword">size_t</span> count);</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数：</p>
<ul>
<li>fd：要写入的文件的文件描述符</li>
<li>buf： 指向内存块的指针，从这个内存块中读取数据写入到文件中</li>
<li>count： 要写入文件的字节个数</li>
</ul>
</li>
<li><p>返回值：如果出现错误，返回-1，如果写入成功，则返回写入到文件中的字节个数</p>
</li>
</ul>
<p>复制文件实例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/types.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/stat.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;fcntl.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ERR_EXIT(m) \</span></span><br><span class="line">        <span class="keyword">do</span> \</span><br><span class="line">        &#123; \</span><br><span class="line">                perror(m); \</span><br><span class="line">                <span class="built_in">exit</span>(EXIT_FAILURE); \</span><br><span class="line">        &#125; <span class="keyword">while</span>(<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> *argv[])</span> </span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (argc != <span class="number">3</span>) &#123;</span><br><span class="line">		<span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">"Usage : %s &lt;sourcefilename&gt; &lt;targetfilename&gt;\n"</span>, argv[<span class="number">0</span>]);</span><br><span class="line">		<span class="built_in">exit</span>(EXIT_FAILURE);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">int</span> infd;</span><br><span class="line">	<span class="keyword">int</span> outfd;</span><br><span class="line">	<span class="keyword">if</span> ((infd = open(argv[<span class="number">1</span>], O_RDONLY)) &lt; <span class="number">0</span>) &#123;</span><br><span class="line">		ERR_EXIT(<span class="string">"open"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> ((outfd = open(argv[<span class="number">2</span>], O_WRONLY | O_CREAT | O_TRUNC, <span class="number">0644</span>)) &lt; <span class="number">0</span>) &#123;</span><br><span class="line">		ERR_EXIT(<span class="string">"open"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">char</span> buf[<span class="number">1024</span>];</span><br><span class="line">	<span class="keyword">int</span> nready;</span><br><span class="line">	<span class="keyword">while</span> ((nready = read(infd, buf, <span class="keyword">sizeof</span>(buf))) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">		write(outfd, buf, nready);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	close(infd);</span><br><span class="line">	close(outfd);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="lseek函数"><a href="#lseek函数" class="headerlink" title="lseek函数"></a>lseek函数</h2><ul>
<li><p>功能说明：通过指定相对于开始位置、当前位置或末尾位置的字节数来重定位文件指针</p>
</li>
<li><p>原型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">off_t</span> lseek (<span class="keyword">int</span> fd, <span class="keyword">off_t</span> offset, <span class="keyword">int</span> base);</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数：</p>
<ul>
<li>fd：文件描述符</li>
<li>offset：需要的偏移量</li>
<li>base：起始的位置</li>
</ul>
</li>
<li><p>返回值：返回新的文件偏移值</p>
</li>
</ul>
<p>base取值：</p>
<table>
<thead>
<tr>
<th style="text-align:left">base</th>
<th style="text-align:left">文件位置</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">SEEK_SET</td>
<td style="text-align:left">从文件开始处计算偏移</td>
</tr>
<tr>
<td style="text-align:left">SEEK_CUR</td>
<td style="text-align:left">从当前文件的偏移值计算偏移</td>
</tr>
<tr>
<td style="text-align:left">SEEK_END</td>
<td style="text-align:left">从文件的结束处计算偏移</td>
</tr>
</tbody>
</table>
<hr>
<h2 id="opendir函数"><a href="#opendir函数" class="headerlink" title="opendir函数"></a>opendir函数</h2><ul>
<li><p>功能：打开一个目录</p>
</li>
<li><p>原型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">DIR *<span class="title">opendir</span><span class="params">(<span class="keyword">char</span> *pathname)</span></span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数：</p>
<ul>
<li>pathname：文件的路径名</li>
</ul>
</li>
<li><p>返回值：打开成功，返回一个目录指针，打开失败，则返回0</p>
</li>
</ul>
<hr>
<p>readdir函数</p>
<ul>
<li><p>功能：访问指定目录中下一个连接的细节</p>
</li>
<li><p>原型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">struct  dirent*  <span class="title">readdir</span><span class="params">(DIR  *dirptr)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">dirent</span> &#123;</span></span><br><span class="line">	<span class="keyword">long</span> d_ino;                 <span class="comment">/* inode number */</span></span><br><span class="line">	<span class="keyword">off_t</span> d_off;                <span class="comment">/* offset to this dirent */</span></span><br><span class="line">	<span class="keyword">unsigned</span> <span class="keyword">short</span> d_reclen;    <span class="comment">/* length of this d_name */</span></span><br><span class="line">	<span class="keyword">char</span> d_name [NAME_MAX+<span class="number">1</span>];   <span class="comment">/* file name (null-terminated) */</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数：</p>
<ul>
<li>dirptr：目录指针</li>
</ul>
</li>
<li><p>返回值：返回一个指向dirent结构的指针，它包含指定目录中下一个连接的细节；<br>  没有更多连接时，返回0</p>
</li>
</ul>
<p>简单版 的ls.c</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/types.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/stat.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;fcntl.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;dirent.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ERR_EXIT(m) \</span></span><br><span class="line">        <span class="keyword">do</span> \</span><br><span class="line">        &#123; \</span><br><span class="line">                perror(m); \</span><br><span class="line">                <span class="built_in">exit</span>(EXIT_FAILURE); \</span><br><span class="line">        &#125; <span class="keyword">while</span>(<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	DIR *dir = opendir(<span class="string">"."</span>);</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">dirent</span> *<span class="title">de</span>;</span></span><br><span class="line">	<span class="keyword">while</span> ((de = readdir(dir)) != <span class="literal">NULL</span>) &#123;</span><br><span class="line">		<span class="keyword">if</span> (<span class="built_in">strcmp</span>(de-&gt;d_name, <span class="string">"."</span>) == <span class="number">0</span> || <span class="built_in">strcmp</span>(de-&gt;d_name, <span class="string">".."</span>) == <span class="number">0</span>) &#123;</span><br><span class="line">			<span class="keyword">continue</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">"%s\n"</span>, de-&gt;d_name);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	closedir(dir);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="closedir函数"><a href="#closedir函数" class="headerlink" title="closedir函数"></a>closedir函数</h2><ul>
<li><p>功能：关闭一个已经打开的目录</p>
</li>
<li><p>原型：</p>
</li>
</ul>
  <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">closedir</span> <span class="params">(DIR  *dirptr)</span></span>;</span><br></pre></td></tr></table></figure>
<ul>
<li><p>参数：</p>
<ul>
<li>dirptr：目录指针</li>
</ul>
</li>
<li><p>返回值：调用成功返回0，失败返回-1</p>
</li>
</ul>
<hr>
<h2 id="mkdir函数"><a href="#mkdir函数" class="headerlink" title="mkdir函数"></a>mkdir函数</h2><ul>
<li><p>功能：用来创建新目录</p>
</li>
<li><p>原型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span>  <span class="title">mkdir</span><span class="params">(<span class="keyword">char</span> *pathname,<span class="keyword">mode_t</span> mode)</span></span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数：</p>
<ul>
<li>pathname：文件的路径名</li>
<li>mode：权限位</li>
</ul>
</li>
<li><p>返回值：调用成功返回0，失败返回-1</p>
</li>
</ul>
<hr>
<h2 id="rmdir函数"><a href="#rmdir函数" class="headerlink" title="rmdir函数"></a>rmdir函数</h2><ul>
<li><p>功能说明：删除一个空目录</p>
</li>
<li><p>原型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span>  <span class="title">rmdir</span><span class="params">(<span class="keyword">char</span> *pathname)</span></span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数：</p>
<ul>
<li>pathname：文件的路径名</li>
</ul>
</li>
<li><p>返回值：调用成功返回0，失败返回-1</p>
</li>
</ul>
<hr>
<h2 id="chmod函数"><a href="#chmod函数" class="headerlink" title="chmod函数"></a>chmod函数</h2><ul>
<li><p>功能说明：改变文件的权限位</p>
</li>
<li><p>原型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span>  <span class="title">chmod</span> <span class="params">(<span class="keyword">char</span> *pathname, <span class="keyword">mode_t</span> mode)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span>  <span class="title">fchmod</span> <span class="params">(<span class="keyword">int</span>  fd, <span class="keyword">mode_t</span> mode)</span></span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数：</p>
<ul>
<li>pathname：文件路径名</li>
<li>mode：权限位</li>
</ul>
</li>
<li><p>返回值：调用成功返回0，失败返回-1</p>
</li>
</ul>
<hr>
<h2 id="chown函数"><a href="#chown函数" class="headerlink" title="chown函数"></a>chown函数</h2><ul>
<li><p>功能：用来改变文件所有者的识别号(owner id)或者它的用户组识别号(group ID)</p>
</li>
<li><p>原型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span>  <span class="title">chown</span> <span class="params">(<span class="keyword">char</span> *pathname, <span class="keyword">uid_t</span> owner,<span class="keyword">gid_t</span> group)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span>  <span class="title">fchown</span> <span class="params">(<span class="keyword">int</span>  fd, <span class="keyword">uid_t</span> owner,<span class="keyword">gid_t</span> group)</span></span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数：</p>
<ul>
<li>pathname：文件的路径名</li>
<li>owner：所有者识别号</li>
<li>group：用户组识别号</li>
</ul>
</li>
<li><p>返回值：调用成功返回0，失败返回-1</p>
</li>
</ul>
<hr>
<h2 id="stat函数"><a href="#stat函数" class="headerlink" title="stat函数"></a>stat函数</h2><p>功能：读取文件元数据</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">stat</span><span class="params">(<span class="keyword">const</span> <span class="keyword">char</span> *path, struct stat *buf)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">fstat</span><span class="params">(<span class="keyword">int</span> fd, struct stat *buf)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">lstat</span><span class="params">(<span class="keyword">const</span> <span class="keyword">char</span> *path, struct stat *buf)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">stat</span> &#123;</span></span><br><span class="line">	<span class="keyword">dev_t</span>     st_dev;     <span class="comment">/* ID of device containing file */</span></span><br><span class="line">	<span class="keyword">ino_t</span>     st_ino;     <span class="comment">/* inode number */</span></span><br><span class="line">	<span class="keyword">mode_t</span>    st_mode;    <span class="comment">/* protection */</span></span><br><span class="line">	<span class="keyword">nlink_t</span>   st_nlink;   <span class="comment">/* number of hard links */</span></span><br><span class="line">	<span class="keyword">uid_t</span>     st_uid;     <span class="comment">/* user ID of owner */</span></span><br><span class="line">	<span class="keyword">gid_t</span>     st_gid;     <span class="comment">/* group ID of owner */</span></span><br><span class="line">	<span class="keyword">dev_t</span>     st_rdev;    <span class="comment">/* device ID (if special file) */</span></span><br><span class="line">	<span class="keyword">off_t</span>     st_size;    <span class="comment">/* total size, in bytes */</span></span><br><span class="line">	<span class="keyword">blksize_t</span> st_blksize; <span class="comment">/* blocksize for file system I/O */</span></span><br><span class="line">	<span class="keyword">blkcnt_t</span>  st_blocks;  <span class="comment">/* number of 512B blocks allocated */</span></span><br><span class="line">	<span class="keyword">time_t</span>    st_atime;   <span class="comment">/* time of last access */</span></span><br><span class="line">	<span class="keyword">time_t</span>    st_mtime;   <span class="comment">/* time of last modification */</span></span><br><span class="line">	<span class="keyword">time_t</span>    st_ctime;   <span class="comment">/* time of last status change */</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>查看文件属性：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/types.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/stat.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;fcntl.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ERR_EXIT(m) \</span></span><br><span class="line">        <span class="keyword">do</span> \</span><br><span class="line">        &#123; \</span><br><span class="line">                perror(m); \</span><br><span class="line">                <span class="built_in">exit</span>(EXIT_FAILURE); \</span><br><span class="line">        &#125; <span class="keyword">while</span>(<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAJOR(a) (int)((unsigned short)a &gt;&gt; 8)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MINOR(a) (int)((unsigned short)a &amp;&amp; 0xFF)</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">filetype</span><span class="params">(struct stat *buf)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">fileperm</span><span class="params">(struct stat *buf, <span class="keyword">char</span> *perm)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> *argv[])</span> </span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (argc != <span class="number">2</span>) &#123;</span><br><span class="line">		<span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">"Usage: %s file\n"</span>, argv[<span class="number">0</span>]);</span><br><span class="line">		<span class="built_in">exit</span>(EXIT_FAILURE);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"Filename: %s\n"</span>, argv[<span class="number">1</span>]);</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">stat</span> <span class="title">sbuf</span>;</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (stat(argv[<span class="number">1</span>], &amp;sbuf) == <span class="number">-1</span>) &#123;</span><br><span class="line">		ERR_EXIT(<span class="string">"stat"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"File number:major %d,minor %d inode %d\n"</span>, MAJOR(sbuf.st_dev), MINOR(sbuf.st_dev), (<span class="keyword">int</span>)sbuf.st_ino);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (filetype(&amp;sbuf)) &#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">"Device number:major %d,minor %d\n"</span>, MAJOR(sbuf.st_rdev), MINOR(sbuf.st_rdev));</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">char</span> perm[<span class="number">11</span>] = &#123;<span class="number">0</span>&#125;;</span><br><span class="line">	fileperm(&amp;sbuf, perm);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"File permission bits=%o %s\n"</span>, sbuf.st_mode &amp; <span class="number">07777</span>, perm);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">filetype</span><span class="params">(struct stat *buf)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">int</span> flag = <span class="number">0</span>;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"Filetype:"</span>);</span><br><span class="line">	<span class="keyword">mode_t</span> mode;</span><br><span class="line">	mode = buf-&gt;st_mode;</span><br><span class="line">	<span class="keyword">switch</span> (mode &amp; S_IFMT) &#123;</span><br><span class="line">	<span class="keyword">case</span> S_IFSOCK:</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">"socket\n"</span>);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> S_IFLNK:</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">"symbolic link\n"</span>);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> S_IFREG:</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">"regular file\n"</span>);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> S_IFBLK:</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">"block device\n"</span>);</span><br><span class="line">		flag = <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> S_IFDIR:</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">"directory\n"</span>);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> S_IFCHR:</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">"character device\n"</span>);</span><br><span class="line">		flag = <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> S_IFIFO:</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">"FIFO\n"</span>);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">default</span>:</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">"unknown file type\n"</span>);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> flag;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">fileperm</span><span class="params">(struct stat *buf, <span class="keyword">char</span> *perm)</span> </span>&#123;</span><br><span class="line">	<span class="built_in">strcpy</span>(perm, <span class="string">"----------"</span>);</span><br><span class="line">	perm[<span class="number">0</span>] = <span class="string">'?'</span>;</span><br><span class="line">	<span class="keyword">mode_t</span> mode;</span><br><span class="line">        mode = buf-&gt;st_mode;</span><br><span class="line">        <span class="keyword">switch</span> (mode &amp; S_IFMT)</span><br><span class="line">        &#123;</span><br><span class="line">        <span class="keyword">case</span> S_IFSOCK:</span><br><span class="line">		perm[<span class="number">0</span>] = <span class="string">'s'</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> S_IFLNK:</span><br><span class="line">		perm[<span class="number">0</span>] = <span class="string">'l'</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> S_IFREG:</span><br><span class="line">		perm[<span class="number">0</span>] = <span class="string">'-'</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> S_IFBLK:</span><br><span class="line">		perm[<span class="number">0</span>] = <span class="string">'b'</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> S_IFDIR:</span><br><span class="line">		perm[<span class="number">0</span>] = <span class="string">'d'</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> S_IFCHR:</span><br><span class="line">		perm[<span class="number">0</span>] = <span class="string">'c'</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> S_IFIFO:</span><br><span class="line">		perm[<span class="number">0</span>] = <span class="string">'p'</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (mode &amp; S_IRUSR)</span><br><span class="line">		perm[<span class="number">1</span>] = <span class="string">'r'</span>;</span><br><span class="line">	<span class="keyword">if</span> (mode &amp; S_IWUSR)</span><br><span class="line">		perm[<span class="number">2</span>] = <span class="string">'w'</span>;</span><br><span class="line">	<span class="keyword">if</span> (mode &amp; S_IXUSR)</span><br><span class="line">		perm[<span class="number">3</span>] = <span class="string">'x'</span>;</span><br><span class="line">	<span class="keyword">if</span> (mode &amp; S_IRGRP)</span><br><span class="line">		perm[<span class="number">4</span>] = <span class="string">'r'</span>;</span><br><span class="line">	<span class="keyword">if</span> (mode &amp; S_IWGRP)</span><br><span class="line">		perm[<span class="number">5</span>] = <span class="string">'w'</span>;</span><br><span class="line">	<span class="keyword">if</span> (mode &amp; S_IXGRP)</span><br><span class="line">		perm[<span class="number">6</span>] = <span class="string">'x'</span>;</span><br><span class="line">	<span class="keyword">if</span> (mode &amp; S_IROTH)</span><br><span class="line">		perm[<span class="number">7</span>] = <span class="string">'r'</span>;</span><br><span class="line">	<span class="keyword">if</span> (mode &amp; S_IWOTH)</span><br><span class="line">		perm[<span class="number">8</span>] = <span class="string">'w'</span>;</span><br><span class="line">	<span class="keyword">if</span> (mode &amp; S_IXOTH)</span><br><span class="line">		perm[<span class="number">9</span>] = <span class="string">'x'</span>;</span><br><span class="line">	perm[<span class="number">10</span>] = <span class="string">'\0'</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="dup函数"><a href="#dup函数" class="headerlink" title="dup函数"></a>dup函数</h2><ul>
<li><p>功能：复制文件描述符</p>
</li>
<li><p>原型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">dup</span><span class="params">(<span class="keyword">int</span> oldfd)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">dup2</span><span class="params">(<span class="keyword">int</span> oldfd, <span class="keyword">int</span> newfd)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">dup3</span><span class="params">(<span class="keyword">int</span> oldfd, <span class="keyword">int</span> newfd, <span class="keyword">int</span> flags)</span></span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数：</p>
<ul>
<li>oldfd：被复制的文件描述符</li>
<li>newfd：指定复制的文件描述符</li>
<li>flags：指定O_CLOEXEC标志</li>
</ul>
</li>
<li><p>返回值：成功返回复制的描述符，失败返回-1</p>
</li>
</ul>
<p><img src="/2019/07/11/linuxfileio/file_dup.png" alt="执行dup前后"></p>
<p>​                                                执行dup前后</p>
<p>dup实现IO重定向：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/types.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/stat.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;fcntl.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ERR_EXIT(m) \</span></span><br><span class="line">        <span class="keyword">do</span> \</span><br><span class="line">        &#123; \</span><br><span class="line">                perror(m); \</span><br><span class="line">                <span class="built_in">exit</span>(EXIT_FAILURE); \</span><br><span class="line">        &#125; <span class="keyword">while</span>(<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="keyword">int</span> fd;</span><br><span class="line">	fd = open(<span class="string">"test.txt"</span>, O_WRONLY);</span><br><span class="line">	<span class="keyword">if</span> (fd == <span class="number">-1</span>) &#123;</span><br><span class="line">		ERR_EXIT(<span class="string">"open"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	close(<span class="number">1</span>);</span><br><span class="line">	dup(fd);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"hello\n"</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="fcntl函数"><a href="#fcntl函数" class="headerlink" title="fcntl函数"></a>fcntl函数</h2><ul>
<li><p>功能：操纵文件描述符，改变已打开的文件的属性</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">fcntl</span><span class="params">(<span class="keyword">int</span> fd, <span class="keyword">int</span> cmd, ... <span class="comment">/* arg */</span> )</span></span>;</span><br><span class="line"><span class="comment">/*文件锁结构体*/</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">flock</span> &#123;</span></span><br><span class="line">	...</span><br><span class="line">	<span class="keyword">short</span> l_type;       <span class="comment">/* Type of lock: F_RDLCK,</span></span><br><span class="line"><span class="comment">			         F_WRLCK, F_UNLCK */</span></span><br><span class="line">	<span class="keyword">short</span> l_whence; <span class="comment">/* How to interpret l_start:</span></span><br><span class="line"><span class="comment">                                   SEEK_SET, SEEK_CUR, SEEK_END */</span></span><br><span class="line">	<span class="keyword">off_t</span> l_start;       <span class="comment">/* Starting offset for lock */</span></span><br><span class="line">	<span class="keyword">off_t</span> l_len;         <span class="comment">/* Number of bytes to lock */</span></span><br><span class="line">	<span class="keyword">pid_t</span> l_pid;        <span class="comment">/* PID of process blocking our lock</span></span><br><span class="line"><span class="comment">                                   (F_GETLK only) */</span></span><br><span class="line">     ...</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数：</p>
<ul>
<li>fd：文件描述符</li>
<li>cmd：指定的命令</li>
</ul>
</li>
<li><p>返回值：失败返回-1</p>
</li>
</ul>
<p>cmd参数：</p>
<ul>
<li>复制文件描述符<ul>
<li>F_DUPFD (long)</li>
</ul>
</li>
<li>文件描述符标志<ul>
<li>F_GETFD (void)</li>
<li>F_SETFD (long)</li>
</ul>
</li>
<li>文件状态标志<ul>
<li>F_GETFL (void)</li>
<li>F_SETFL (long)</li>
</ul>
</li>
<li>文件锁<ul>
<li>F_GETLK</li>
<li>F_SETLK，F_SETLKW</li>
</ul>
</li>
</ul>
<p>更改状态信息：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/types.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/stat.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;fcntl.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ERR_EXIT(m) \</span></span><br><span class="line">        <span class="keyword">do</span> \</span><br><span class="line">        &#123; \</span><br><span class="line">                perror(m); \</span><br><span class="line">                <span class="built_in">exit</span>(EXIT_FAILURE); \</span><br><span class="line">        &#125; <span class="keyword">while</span>(<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">set_flag</span><span class="params">(<span class="keyword">int</span> fd, <span class="keyword">int</span> flags)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">clr_flag</span><span class="params">(<span class="keyword">int</span> fd, <span class="keyword">int</span> flags)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	set_flag(<span class="number">0</span>, O_NONBLOCK);</span><br><span class="line">	<span class="keyword">int</span> ret;</span><br><span class="line">	<span class="keyword">char</span> buf[<span class="number">1024</span>];</span><br><span class="line">	ret = read(<span class="number">0</span>, buf, <span class="number">1024</span>);</span><br><span class="line">	<span class="keyword">if</span> (ret == <span class="number">-1</span>) &#123;</span><br><span class="line">		ERR_EXIT(<span class="string">"read"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"buf = %s\n"</span>, buf);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">set_flag</span><span class="params">(<span class="keyword">int</span> fd, <span class="keyword">int</span> flags)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">int</span> val;</span><br><span class="line">	val = fcntl(fd, F_GETFL, <span class="number">0</span>);</span><br><span class="line">	<span class="keyword">if</span> (val == <span class="number">-1</span>) &#123;</span><br><span class="line">		ERR_EXIT(<span class="string">"fcntl"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">int</span> ret;</span><br><span class="line">	ret = fcntl(fd, F_SETFL, val | flags);</span><br><span class="line">	<span class="keyword">if</span> (ret == <span class="number">-1</span>) &#123;</span><br><span class="line">		ERR_EXIT(<span class="string">"fcntl"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">clr_flag</span><span class="params">(<span class="keyword">int</span> fd, <span class="keyword">int</span> flags)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">int</span> val;</span><br><span class="line">	val = fcntl(fd, F_GETFL, <span class="number">0</span>);</span><br><span class="line">	<span class="keyword">if</span> (val == <span class="number">-1</span>) &#123;</span><br><span class="line">		ERR_EXIT(<span class="string">"fcntl"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">int</span> ret;</span><br><span class="line">	ret = fcntl(fd, F_SETFL, val &amp; ~flags);</span><br><span class="line">	<span class="keyword">if</span> (ret == <span class="number">-1</span>) &#123;</span><br><span class="line">		ERR_EXIT(<span class="string">"fcntl"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>文件锁：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/types.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/stat.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;fcntl.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ERR_EXIT(m) \</span></span><br><span class="line">        <span class="keyword">do</span> \</span><br><span class="line">        &#123; \</span><br><span class="line">                perror(m); \</span><br><span class="line">                <span class="built_in">exit</span>(EXIT_FAILURE); \</span><br><span class="line">        &#125; <span class="keyword">while</span>(<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="keyword">int</span> fd;</span><br><span class="line">	<span class="keyword">if</span> ((fd = open(<span class="string">"test2.txt"</span>, O_CREAT | O_RDWR | O_TRUNC, <span class="number">0666</span>)) == <span class="number">-1</span>) &#123;</span><br><span class="line">		ERR_EXIT(<span class="string">"open"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">flock</span> <span class="title">lock</span>;</span></span><br><span class="line">	<span class="built_in">memset</span>(&amp;lock, <span class="number">0</span>, <span class="keyword">sizeof</span>(lock));</span><br><span class="line">	lock.l_type = F_WRLCK;</span><br><span class="line">	lock.l_whence = SEEK_SET;</span><br><span class="line">	lock.l_start = <span class="number">0</span>;</span><br><span class="line">	lock.l_len = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> ((fcntl(fd, F_SETLK, &amp;lock)) == <span class="number">0</span>) &#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">"lock success\n"</span>);</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">"press any key to unlock\n"</span>);</span><br><span class="line">		getchar();</span><br><span class="line">		lock.l_type = F_UNLCK;</span><br><span class="line">		<span class="keyword">if</span> (fcntl(fd, F_SETLK, &amp;lock) == <span class="number">0</span>) &#123;</span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">"unlock success\n"</span>);</span><br><span class="line">		&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">			ERR_EXIT(<span class="string">"unlock fail"</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		ERR_EXIT(<span class="string">"lock fail"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/07/11/linuxcomputersystem/" rel="next" title="计算机系统组成">
                <i class="fa fa-chevron-left"></i> 计算机系统组成
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/07/11/linuxprocess/" rel="prev" title="Linux进程">
                Linux进程 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">LuQiLin</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">80</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#什么是I-O？"><span class="nav-number">1.</span> <span class="nav-text">什么是I/O？</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#文件描述符"><span class="nav-number">2.</span> <span class="nav-text">文件描述符</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#文件共享"><span class="nav-number">3.</span> <span class="nav-text">文件共享</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#文件描述符与文件指针转换"><span class="nav-number">4.</span> <span class="nav-text">文件描述符与文件指针转换</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#文件调用系统"><span class="nav-number">5.</span> <span class="nav-text">文件调用系统</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#open函数"><span class="nav-number">5.1.</span> <span class="nav-text">open函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#close函数"><span class="nav-number">5.2.</span> <span class="nav-text">close函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#create函数"><span class="nav-number">5.3.</span> <span class="nav-text">create函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#read函数"><span class="nav-number">5.4.</span> <span class="nav-text">read函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#write函数"><span class="nav-number">5.5.</span> <span class="nav-text">write函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#lseek函数"><span class="nav-number">5.6.</span> <span class="nav-text">lseek函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#opendir函数"><span class="nav-number">5.7.</span> <span class="nav-text">opendir函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#closedir函数"><span class="nav-number">5.8.</span> <span class="nav-text">closedir函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#mkdir函数"><span class="nav-number">5.9.</span> <span class="nav-text">mkdir函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#rmdir函数"><span class="nav-number">5.10.</span> <span class="nav-text">rmdir函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#chmod函数"><span class="nav-number">5.11.</span> <span class="nav-text">chmod函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#chown函数"><span class="nav-number">5.12.</span> <span class="nav-text">chown函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#stat函数"><span class="nav-number">5.13.</span> <span class="nav-text">stat函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#dup函数"><span class="nav-number">5.14.</span> <span class="nav-text">dup函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#fcntl函数"><span class="nav-number">5.15.</span> <span class="nav-text">fcntl函数</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LuQiLin</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
